<template>
    <zh-app style="font-family: '阿里巴巴普惠体'">
        <!-- 导航栏 -->
		<zh-navbar title="调剂申请" color="#85a5ff" linear="right"/>
        <view style="margin-top: 20rpx;">
        </view>
        <zh-sheet v-if="data.tjSchoolList.length<=0">
            <zh-result
                btnText="返回"
                :title="data.title"
                :status="data.status"
                :subTitle="data.subTitle"
                @click="goBack"
            ></zh-result>
        </zh-sheet>
        <zh-card v-if="!data.showSuccess" :title="'调剂第'+item.xh+'志愿'" v-for="(item, index) in data.toSchoolIdList" :key="index" >
            <template v-slot:content>
                <zh-select mode="single" v-model="item.schoolId" type="list" :fieldNames="{ label: 'schoolName', value: 'schoolId' }"  :options="data.tjSchoolList" :placeholder="'请选择第'+item.xh+'志愿学校'"></zh-select>
            </template>
        </zh-card>
        <view class="flex flex-row" v-if="!data.showSuccess&&data.tjSchoolList.length>0">
			<view class="flex-1 mr-32" style="margin: 0 auto">
			    <zh-button label="提交" block @click="save" :loading="data.loading" :disabled="data.loading"
						color="#85a5ff"
						linear="right"></zh-button>
			</view>
		</view>
        <zh-sheet v-if="data.showSuccess">
            <zh-result
                
                color="green" 
                status="success"
                btnText="返回"
                title="申请成功"
                subTitle="您的调剂申请已申请成功"
                @click="goBack"
            ></zh-result>
        </zh-sheet>
        <zh-message ref="messageRef" :duration="3000" :width="400" :height="400" :lines="5"></zh-message>
    </zh-app>
</template>
<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app";
import {reactive,ref} from "vue";
const data = reactive({
	tjSchoolList:[],
    toSchoolIdList:[] as any,
    studentEnrollId:'',
    loading:false,
    showSuccess:false,
    title:"数据加载中",
    subTitle:"请稍后",
    status:"empty"

})
let messageRef = ref();
// 初始化
onLoad((options:any) => {
    if(options.studentEnrollId){
        data.studentEnrollId=options.studentEnrollId
        getSchoolList()
    }
		
})
const getSchoolList=()=>{
    const params = { studentEnrollId: data.studentEnrollId };
    $api.common.send.post("eduSchoolTj0009", params).then((res: any) => {
        data.tjSchoolList=res.result||[]
        if(data.tjSchoolList.length<=0){
            data.title="没有可调剂的学校"
            data.subTitle="如有疑问请致电学校了解情况"
            return
        }
        let idList=[] as any
        let i=1;
        data.tjSchoolList.forEach((item)=>{
            let param={schoolId:'',xh:i}
            idList.push(param);
            i=i+1;
        })
        data.toSchoolIdList=idList
    }).catch((error:any)=>{
        if(error.code==2){
            data.title="已申请"
            data.subTitle=error.message
            data.status="warning"
        }else if(error.code==3){
            data.title="已申请"
            data.subTitle=error.message
            data.status="warning"
        }else if(error.code==4){
            data.title=error.message
            data.status="lock"
            data.subTitle="请关注东阳教育微信公众号了解情况"
        }else{
            data.title="系统异常" 
            data.status="error"
            data.subTitle=error.message
        }
    })

}
const goBack=()=>{
	$router.back(1);
}
//保存
const save=()=>{
    let messageData: any = {};
    messageData.model = 'warn';
    
    if(data.toSchoolIdList.length<=0){
        messageData.text = '请先选择调剂志愿学校'; 
        messageRef.value.show(messageData);
        return;
    }
    let flag=true;
    let xh=1;
    let saveList=[] as any
    let checkFlag=false;
    data.toSchoolIdList.forEach((item:any)=>{
        if(!checkFlag){
            if(!item.schoolId){
                if(item.xh===1){
                    messageData.text = '请先选择调剂第'+item.xh+'志愿学校'; 
                    messageRef.value.show(messageData);
                    checkFlag=true;
                    return;
                }else{
                    flag=false
                }
                    
            }else{
                if(!flag){
                    messageData.text = '请先选择调剂第'+(item.xh-1)+'志愿学校'; 
                    messageRef.value.show(messageData);
                    checkFlag=true;
                    return;
                }
                if(saveList.includes(item.schoolId)){
                    messageData.text = '各个志愿之间选择的学校有重复，请仔细检查'; 
                    messageRef.value.show(messageData);
                    checkFlag=true;
                    return;
                }
                saveList.push(item.schoolId)
            }
        }
    })
    if(checkFlag){
        return
    }
    let params={studentEnrollId:data.studentEnrollId,toSchoolIdList:saveList}
    data.loading=true;
    $api.common.send.post("studentTj0007", params).then((res: any) => {
        data.loading=false;
        data.showSuccess=true
    }).catch(()=>{
        data.loading=false;
    })

}
</script>